/*
 * Copyright (c) 2025 Industrial Software Feature Database
 */
package com.comac.ins.isfd.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.comac.ins.common.core.domain.R;
import com.comac.ins.common.core.exception.base.BaseException;
import com.comac.ins.common.core.utils.StringUtils;
import com.comac.ins.common.idempotent.annotation.RepeatSubmit;
import com.comac.ins.common.log.annotation.Log;
import com.comac.ins.common.log.enums.BusinessType;
import com.comac.ins.common.mybatis.core.page.TableDataInfo;
import com.comac.ins.common.web.core.BaseController;
import com.comac.ins.isfd.constant.IsfdMeshModelConstants;
import com.comac.ins.isfd.domain.bo.IsfdMeshModelBatchDownLoadBo;
import com.comac.ins.isfd.domain.bo.IsfdMeshModelSearchBo;
import com.comac.ins.isfd.domain.bo.IsfdMeshQueryBo;
import com.comac.ins.isfd.domain.vo.IsfdDownLoadVo;
import com.comac.ins.isfd.domain.vo.IsfdMeshModelSearchShowVo;
import com.comac.ins.isfd.domain.vo.IsfdMeshModelStructureDefinitionTreeNodeVo;
import com.comac.ins.isfd.domain.vo.IsfdModelManagementRenderingResultVo;
import com.comac.ins.isfd.service.IIsfdMeshModelService;
import com.comac.ins.isfd.util.bdfparser.entities.bdf.BDFEntityShowVo;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.bson.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;


/**
 * 自然网格模型库
 *
 * @author hxloongs
 * @date 2025-01-16
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/isfd/mesh")
public class IsfdMeshModelController extends BaseController {


    @Autowired
    private IIsfdMeshModelService iIsfdMeshModelService;

    /**
     * 自然网格模型-左侧树查询
     */
    @SaCheckPermission("isfd:mesh:tree")
    @GetMapping("/tree")
    public R<List<IsfdMeshModelStructureDefinitionTreeNodeVo>> tree(@RequestParam(value = "sectionName", required = false) String sectionName) {
        return R.ok(iIsfdMeshModelService.tree(sectionName));
    }

    /**
     * 自然网格模型-分页查询
     */
    @SaCheckPermission("isfd:mesh:list")
    @PostMapping("/list")
    public TableDataInfo<Document> queryTaskList(@Validated @RequestBody IsfdMeshQueryBo query) {
        // 校验 fileName 是否包含非法字符
        String fileName = query.getFileName();
        if (StringUtils.isNotEmpty(fileName)) {
            for (char illegalChar : IsfdMeshModelConstants.ILLEGAL_CHARS.toCharArray()) {
                if (fileName.contains(String.valueOf(illegalChar))) {
                    throw new BaseException("文件名暂时不支持字符: " + illegalChar + "的检索");
                }
            }
        }
        return iIsfdMeshModelService.queryList(query);
    }

    /**
     * 自然网格模型-文件预览
     */
    @SaCheckPermission("isfd:mesh:preview")
    @GetMapping("/file/preview")
    public R<BDFEntityShowVo> getFilePreview(@RequestParam("_id") String id) {
        return R.ok(iIsfdMeshModelService.getBDFPreview(id));
    }

    /**
     * 自然网格模型-文件搜索
     */
    @SaCheckPermission("isfd:mesh:search")
    @PostMapping("/file/search")
    public R<IsfdMeshModelSearchShowVo> search(@RequestBody IsfdMeshModelSearchBo bo) {
        return R.ok(iIsfdMeshModelService.searchBDFContent(bo));
    }

    /**
     * 自然网格模型-获取下载文件的信息
     */
    @SaCheckPermission("isfd:mesh:download")
    @GetMapping("/file/download/detail")
    public R<IsfdDownLoadVo> downloadDetail(@RequestParam("_id") String id) {
        return R.ok(iIsfdMeshModelService.downloadDetail(id));
    }


    /**
     * 自然网格模型-文件下载
     */
    @SaCheckPermission("isfd:mesh:download")
    @RepeatSubmit()
    @GetMapping("/file/download")
    public void download(@RequestParam("_id") String id, HttpServletResponse response) {
        iIsfdMeshModelService.newDownload(id, response);
    }

    /**
     * 自然网格模型-批量文件下载
     */
    @SaCheckPermission("isfd:mesh:download")
    @RepeatSubmit()
    @PostMapping("/file/batch/download")
    public void batchDownload(@RequestBody List<String> ids, HttpServletResponse response) {
        iIsfdMeshModelService.batchDownload(ids, response);
    }

    /**
     * 自然网格模型-批量文件下载
     * 可设置zip包名称
     */
    @SaCheckPermission("isfd:mesh:download")
    @RepeatSubmit()
    @PostMapping("/file/batch/downloadWithName")
    public void batchDownload(@RequestBody IsfdMeshModelBatchDownLoadBo batchDownLoadBo, HttpServletResponse response) {
        iIsfdMeshModelService.batchDownloadWithName(batchDownLoadBo, response);
    }


    /**
     * 自然网格模型-删除
     */
    @SaCheckPermission("isfd:mesh:download")
    @RepeatSubmit()
    @Log(title = "自然网格模型-删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public R<Void> remove(@NotNull(message = "主键不能为空")
                          @PathVariable String id) {
        return toAjax(iIsfdMeshModelService.deleteWithValidById(id));
    }

    /**
     * 自然网格模型-重新上传
     */
    @SaCheckPermission("isfd:mesh:download")
    @RepeatSubmit()
    @Log(title = "自然网格模型-重新上传", businessType = BusinessType.IMPORT)
    @PostMapping(value = "/reUpload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Void> reUpload(@RequestParam("file") MultipartFile file,
                            @NotNull(message = "主键不能为空") @RequestParam("_id") String id) {
        return toAjax(iIsfdMeshModelService.reUpload(file, id));
    }


    /**
     * 模型渲染
     */
    @Log(title = "模型渲染", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @GetMapping("/load")
    public R<IsfdModelManagementRenderingResultVo> loadModel(@RequestParam("_id") String id) {
        return R.ok(iIsfdMeshModelService.loadModel(id));
    }

}
